package com.lan.workflow.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lan.workflow.entity.LeaveApply;
import com.lan.workflow.mapper.LeaveApplyMapper;
import com.lan.workflow.service.ILeaveApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lanwp
 * @since 2020-08-13
 */
@Service
public class LeaveApplyServiceImpl extends ServiceImpl<LeaveApplyMapper, LeaveApply> implements ILeaveApplyService {

    @Autowired
    private LeaveApplyMapper leaveApplyMapper;
    @Autowired
    private IdentityService identityService;
    @Autowired
    private RuntimeService runtimeService;


    @Transactional(rollbackFor = Exception.class)
    public ProcessInstance startWorkflow(LeaveApply leaveApply, String userid, Map<String, Object> variables) {
        String businesskey = String.valueOf(leaveApply.getId());//使用leaveapply表的主键作为businesskey,连接业务数据和流程数据
        identityService.setAuthenticatedUserId(userid);
        ProcessInstance instance = runtimeService.startProcessInstanceByKey("leave_apply",businesskey, variables);
        return instance;
    }

    @Override
    public IPage<LeaveApply> page(IPage page, Map<String, Object> paramMap) {
        return leaveApplyMapper.page(page, paramMap);
    }

}
